<?php

include'Utils.php';

class VideosController extends AppController{
	
	/**
	 * List components used in this class
	 */
	public $components = array('CenterInfo', 'Subject', 'Course');
	
	/**
	 * Front-End function: display list videos
	 */
	function index(){
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		// Get Course
		$courses = $this->Course->getAll();
		$this->set('courses', $courses);
		
		$subId = isset($_GET['sub_id']) ? $_GET['sub_id'] : "";
		If($subId != ""){
	
			$totalRec = $this->Video->find('count', array(
				'conditions' => array('Video.m_video_sub_id' => $subId)
			));
			
			$pageNo = isset($_GET['page_no']) ? $_GET['page_no'] : 1;
			$startPage = isset($_GET['start_page']) ? $_GET['start_page'] : 1;
			$endPage = isset($_GET['end_page']) ? $_GET['end_page'] : $startPage + 4;
			$pagerInfo = array();
			$pagerInfo = Utils::getPagerInfo($pageNo, $totalRec, $startPage, $endPage);
			$videoList = $this->Video->find('all', array(
				'condition' => array('Video.m_video_sub_id' => $subId),
				'order' => 'Video.m_video_id ASC',
				'limit' => 5,
				'page' => $pagerInfo['start_index']
			));
			$this->set('listVideos', $videoList);
			$this->set('pagerInfo', $pagerInfo);
			$this->set('selectedSubId', $subId);
			
		}
	}
	
	/**
	 * Front-End function: display view video page
	 */
	function view(){
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		// Get list Course
		$courses = $this->Course->getAll();
		$this->set('courses', $courses);
		
		$id = isset($_GET["id"]) ? $_GET["id"] : "";
		if ($id != ""){
			$this->set('video', $this->Video->findAllBym_video_id($id));
		}else{
			$this->redirect(array(
				'action' => 'index'
			));
		}
	}
	
	/**
	 * Admin function used to display list videos page
	 */
	function manageListVideos(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		if ( $subId == "" ){
			$subId = isset($_GET['subId']) ? $_GET['subId'] : "";
		}
		If($subId != ""){
			$totalRec = $this->Video->find('count', array(
				'conditions' => array("Video.m_video_sub_id" => $subId,
									  "Video.m_is_main_video <> " => '1')
			));
			$pageNo = isset($_GET['page_no']) ? $_GET['page_no'] : 1;
			$startPage = isset($_GET['start_page']) ? $_GET['start_page'] : 1;
			$endPage = isset($_GET['end_page']) ? $_GET['end_page'] : $startPage + 4;
			$pagerInfo = array();
			$pagerInfo = Utils::getPagerInfo($pageNo, $totalRec, $startPage, $endPage);
			$videoList = $this->Video->find('all', array(
				'conditions' => array('Video.m_video_sub_id' => $subId,
									  "Video.m_is_main_video <> " => '1'),
				'order' => 'Video.m_video_id ASC',
				'limit' => 5,
				'page' => $pagerInfo['start_index']
			));
			$this->set('listVideos', $videoList);
			$this->set('pagerInfo', $pagerInfo);
			$this->set('selectedSubId', $subId);
			
		}
	}
	
	/**
	 * Admin function used to display edit video page
	 */
	function edit(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		
		// Get GET data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		
		$id = isset($_POST['id']) ? $_POST['id'] : "";
		
		$isBack = isset($_POST['isBack']) ? $_POST['isBack'] : "";

		if($id != ""){
			if($isBack != ""){
				// Get POST data
				$title = isset($_POST['title']) ? $_POST['title'] : "";
				$title = htmlentities($title, ENT_QUOTES, 'utf-8');
				$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
				$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
				$descript = htmlentities($descript, ENT_QUOTES, 'utf-8');
				// Set display data
				$this->set("selectedId", $subId);
				$this->set("id", $id);
				$this->set("title", $title);
				$this->set("youtubeId", $youtubeId);
				$this->set("descript", $descript);
				$this->set("isBack", $isBack);
			}else{
				$listVideos = $this->Video->findAllBym_video_id($id);
				$this->set("video", $listVideos[0]);
				$this->set("selectedId", $subId);
				$this->set("id", $id);
				$this->set("isBack", $isBack);
			}
		}else{
			$this->redirect(array(
				'action' => 'manageListVideos'
			));
		}
		
	}
	
	/**
	 * Admin function used to display confirm edit video page
	 */
	function confirmEdit(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		
		// Get POST data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		$id = isset($_POST['id']) ? $_POST['id'] : "";
		$title = isset($_POST['title']) ? $_POST['title'] : "";
		$title = htmlentities($title, ENT_QUOTES, 'utf-8');
		$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
		$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
		$descript = htmlentities($descript, ENT_QUOTES, 'utf-8');
		// Set display data
		$this->set("subId", $subId);
		$this->set("id", $id); 
		$this->set("title", $title);
		$this->set("youtubeId", $youtubeId);
		$this->set("descript", $descript);
	}
	
	/**
	 * Admin function used to update video
	 */
	function registEdit(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);	
		
		// Get POST data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		$id = isset($_POST['id']) ? $_POST['id'] : "";
		$title = isset($_POST['title']) ? $_POST['title'] : "";
		$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
		$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
		
		if($id == "" || $subId == "" || $title == "" || $youtubeId == "" || $descript =="" ){
			$this->redirect(array(
				'action' => 'manageListVideos'
			));
		}
		
		$savedData = array(
			'Video.m_youtube_id' => "'". $youtubeId ."'",
			'Video.m_video_title' => "'". htmlentities($title, ENT_QUOTES, 'utf-8') . "'",
			'Video.m_video_descript' => "'". htmlentities($descript, ENT_QUOTES, 'utf-8') . "'"
		);
		
		$saveCondition = array(
			'Video.m_video_id' => $id
		);
		
		if($this->Video->updateAll($savedData, $saveCondition)){
			$this->redirect(array('action' => "manageListVideos?subId=$subId"));
		}
	}
	
	/**
	 * Admin function used to display add video page
	 */
	function add(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);

		// Get POST data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		$isBack = isset($_POST['isBack']) ? $_POST['isBack'] : "";
		$this->set("selectedId", $subId);
		$this->set('isBack', $isBack);
		if($isBack != ""){
			// Get POST data
			$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
			$title = isset($_POST['title']) ? $_POST['title'] : "";
			$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
			$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
			
			// Set displayed data
			$this->set("subId", $subId);
			$this->set("title", htmlentities($title, ENT_QUOTES, 'utf-8'));
			$this->set("youtubeId", $youtubeId);
			$this->set("descript", htmlentities($descript, ENT_QUOTES, 'utf-8'));
		}
	}
	
	/**
	 * Admin function used to display confirm add video page
	 */
	function confirmAdd(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get POST data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		$title = isset($_POST['title']) ? $_POST['title'] : "";
		$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
		$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
		
		// Set displayed data
		$this->set("subId", $subId);
		$this->set("title", htmlentities($title, ENT_QUOTES, 'utf-8'));
		$this->set("youtubeId", $youtubeId);
		$this->set("descript", htmlentities($descript, ENT_QUOTES, 'utf-8'));
	}
	
	/**
	 * Admin function used to add new video
	 */
	function registAdd(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get POST data
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		$title = isset($_POST['title']) ? $_POST['title'] : "";
		$youtubeId = isset($_POST['youtubeId']) ? $_POST['youtubeId'] : "";
		$descript = isset($_POST['descript']) ? $_POST['descript'] : "";
		
		if($subId == "" || $title == "" || $youtubeId == "" || $descript == ""){
			$this->redirect(array(
				'action' => 'manageListVideos'
			));
		}
		
		// Set saved data
		$this->request->data['m_video_id'] = null;
		$this->request->data['m_video_sub_id'] = $subId;
		$this->request->data['m_youtube_id'] = $youtubeId;
		$this->request->data['m_video_title'] = htmlentities($title, ENT_QUOTES, 'utf-8');
		$this->request->data['m_video_descript'] = htmlentities($descript, ENT_QUOTES, 'utf-8');
		$this->request->data['m_is_main_video'] = '0';
		
		// Regist data
		if($this->Video->save($this->request->data)){
			$this->redirect(array('action' => "manageListVideos?subId=$subId"));
		}
	}
	
	/**
	 * Admin function used to delete video
	 */
	public function delete(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get POST data
		$id = isset($_POST['id']) ? $_POST['id'] : "";
		$subId = isset($_POST['subId']) ? $_POST['subId'] : "";
		if($id != ""){
			if($this->Video->deleteAll(array('Video.m_video_id' => $id), false)){
				$this->redirect(array('action' => "manageListVideos?subId=$subId"));
			}
		}else{
			$this->redirect(array(
				'action' => 'manageListVideos'
			));
		}
	}
}

?>